package com.srit.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.srit.core.bean.Page;
import com.srit.system.bean.EffectTime;
import com.srit.system.dao.EffectTimeDao;

@Service
@Transactional
public class EffectTimeDaoImpl implements EffectTimeDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public void save(EffectTime effectTime) {
		String sql = "insert into TC_EFFECTTIME (ID,GROUPID,SECTION,INTERVAL,STARTHOUR,MINUTES) values(SEQ_TC_EFFECTTIME.NEXTVAL,?,?,?,?,?) ";
		jdbcTemplate.update(sql,
				new Object[] { effectTime.getGroupId(),
						effectTime.getSection(), effectTime.getInterval(),
						effectTime.getStartHour(), effectTime.getMinutes() });
	}

	@Override
	public void update(EffectTime effectTime) {
		String sql = "update  TC_EFFECTTIME set SECTION=?,INTERVAL=?,STARTHOUR=?,MINUTES=? where id=?";
		jdbcTemplate.update(
				sql,
				new Object[] { effectTime.getSection(),
						effectTime.getInterval(), effectTime.getStartHour(),
						effectTime.getMinutes(), effectTime.getId() });
	}

	@Override
	public EffectTime getById(Integer id) {
		String sql = "select * from TC_EFFECTTIME where id=?";
		return jdbcTemplate.queryForObject(sql, new Object[] { id },
				new EffectTimeRowMapper());
	}

	@Override
	public List<EffectTime> listByGroupId(int groupId) {
		String sql = "select * from TC_EFFECTTIME where GROUPID=?";
		return jdbcTemplate.query(sql, new Object[] { groupId },
				new EffectTimeRowMapper());
	}

	@Override
	public void delete(Integer id) {
		String sql = "delete from TC_EFFECTTIME where id=?";
		jdbcTemplate.update(sql, new Object[] { id });
	}

	@Override
	public int getMaxId() {
		String sql = "select max(id) from TC_EFFECTTIME";
		return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public void page(Page page) {
		// TODO Auto-generated method stub
		
	}

}

class EffectTimeResultSetExtractor implements ResultSetExtractor {
	@Override
	public Object extractData(ResultSet rs) throws SQLException,
			DataAccessException {
		EffectTime effectTime = new EffectTime();
		effectTime.setGroupId(rs.getInt("GROUPID"));
		effectTime.setId(rs.getInt("ID"));
		effectTime.setSection(rs.getInt("SECTION"));
		effectTime.setInterval(rs.getInt("INTERVAL"));
		effectTime.setStartHour(rs.getInt("STARTHOUR"));
		effectTime.setMinutes(rs.getInt("MINUTES"));
		return effectTime;
	}
}

class EffectTimeRowMapper implements RowMapper {

	@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		EffectTimeResultSetExtractor extractor = new EffectTimeResultSetExtractor();
		return extractor.extractData(rs);
	}

}
